<template>
  <div class="visit-detail">
    <div class="page-header">
      <div class="left">
        <h2>拜访详情</h2>
      </div>
      <div class="right">
        <el-button @click="goBack">返回</el-button>
      </div>
    </div>

    <el-card v-loading="loading" class="detail-card">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="拜访主题" :span="2">
          {{ visitDetail.title }}
        </el-descriptions-item>
        <el-descriptions-item label="客户名称">
          {{ visitDetail.customerName }}
        </el-descriptions-item>
        <el-descriptions-item label="拜访状态">
          <el-tag :type="getStatusType(visitDetail.visitStatusName)" size="small">
            {{ visitDetail.visitStatusName }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="拜访时间">
          {{ formatTime(visitDetail.visitTime) }}
        </el-descriptions-item>
        <el-descriptions-item label="拜访类型">
          {{ getVisitTypeLabel(visitDetail.visitType) }}
        </el-descriptions-item>
        <el-descriptions-item label="拜访地点" :span="2">
          {{ visitDetail.visitAddress }}
        </el-descriptions-item>
        <el-descriptions-item label="拜访目的" :span="2">
          {{ visitDetail.visitPurpose }}
        </el-descriptions-item>
        <el-descriptions-item label="拜访内容" :span="2">
          <div class="content-text">{{ visitDetail.content }}</div>
        </el-descriptions-item>
        <el-descriptions-item label="拜访结果" :span="2">
          <div class="content-text">{{ visitDetail.visitResult }}</div>
        </el-descriptions-item>
        <el-descriptions-item label="后续计划" :span="2">
          <div class="content-text">{{ visitDetail.nextPlan }}</div>
        </el-descriptions-item>
        <el-descriptions-item label="协同人员" :span="2">
          <el-tag
            v-for="collaborator in visitDetail.collaborators"
            :key="collaborator.id"
            size="small"
            class="collaborator-tag"
          >
            {{ collaborator.name }}
          </el-tag>
          <span v-if="!visitDetail.collaborators || visitDetail.collaborators.length === 0">
            无
          </span>
        </el-descriptions-item>
        <el-descriptions-item label="创建人">
          {{ visitDetail.createdBy }}
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">
          {{ formatTime(visitDetail.createdAt) }}
        </el-descriptions-item>
        <el-descriptions-item label="更新时间">
          {{ formatTime(visitDetail.updatedAt) }}
        </el-descriptions-item>
      </el-descriptions>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getVisit } from '@/api/visit'
import { VisitTypeMap } from '@/types/visit'
import type { Visit } from '@/types/visit'
import axios from 'axios'
import { formatUnixTimestamp } from '@/utils/date'

const route = useRoute()
const router = useRouter()
const loading = ref(false)

interface VisitDetailData extends Omit<Visit, 'visitTime'> {
  visitTime?: number
  visitStatusName?: string
  formattedVisitTime?: string
  collaborators?: Array<{ id: number; name: string }>
}

const visitDetail = ref<VisitDetailData>({
  customerId: 0,
  title: '',
  visitType: '',
  visitPurpose: '',
  visitAddress: '',
  content: '',
  visitResult: '',
  nextPlan: '',
})

const getStatusType = (status?: string) => {
  if (!status) return 'info'
  const statusTypes: Record<string, string> = {
    计划中: 'info',
    已完成: 'success',
    已取消: 'danger',
  }
  return statusTypes[status] || 'info'
}

// 获取拜访类型的中文标签
const getVisitTypeLabel = (visitType?: string) => {
  if (!visitType) return '-'
  return VisitTypeMap[visitType as keyof typeof VisitTypeMap]?.label || visitType
}

// 格式化时间戳
const formatTime = (timestamp: number | string | undefined): string => {
  if (!timestamp) return '-'
  return formatUnixTimestamp(typeof timestamp === 'string' ? parseInt(timestamp) : timestamp)
}

const loadVisitDetail = async () => {
  loading.value = true
  try {
    const res = await getVisit(Number(route.params.id))
    visitDetail.value = res.data
  } catch (error) {
    console.error('加载拜访详情失败:', error)
    ElMessage.error('加载拜访详情失败')
  } finally {
    loading.value = false
  }
}

// 记录页面访问日志
const recordPageAccess = async () => {
  try {
    await axios.post('/api/logs', {
      action: 'ACCESS_PAGE',
      description: `访问拜访详情页面 - ID: ${route.params.id}`,
    })
  } catch (error) {
    console.error('记录页面访问日志失败:', error)
  }
}

const goBack = () => {
  router.push('/visits')
}

onMounted(() => {
  loadVisitDetail()
  recordPageAccess()
})
</script>

<style scoped>
.visit-detail {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 0 20px;
  height: 56px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.page-header .left h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 500;
  color: #1f2f3d;
}

.detail-card {
  background-color: #fff;
}

.content-text {
  line-height: 1.6;
  max-height: 200px;
  overflow-y: auto;
  white-space: pre-wrap;
}

.collaborator-tag {
  margin-right: 8px;
  margin-bottom: 4px;
}
</style>
